package com.liddhome.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.liddhome.entity.Essay;
import com.liddhome.entity.PageBean;
import com.liddhome.service.EssayService;
import com.liddhome.util.DateUtil;
import com.liddhome.util.ResponseUtil;
import com.liddhome.util.StringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
/**
 * 心情随笔管理员Controller(控制)层
 * @author lidd
 *
 */
@Controller
@RequestMapping("/admin/essay")
public class EssayAdminController {

	@Resource
	private EssayService essayService;
	
	/**
	 * 增加或修改心情随笔内容
	 * @param essay
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public String save(Essay essay,HttpServletResponse response)throws Exception{
		int resultTotal=0; // 操作的记录条数
		if(essay.getId()==null){
			//添加心情随笔
			essay.setId(DateUtil.getCurrentDateStr());
			resultTotal=essayService.add(essay);
		}else{
			//修改心情随笔
			resultTotal=essayService.update(essay);
		}
		JSONObject result=new JSONObject();
		if(resultTotal>0){
			result.put("success", true);
		}else{
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 分页查询心情随笔信息
	 * @param page
	 * @param rows
	 * @param s_customer
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public String list(@RequestParam(value="page",required=false)String page,@RequestParam(value="rows",required=false)String rows,String s_createDate,HttpServletResponse response)throws Exception{
		PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("createDate", StringUtil.formatLike(s_createDate));
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		List<Essay> blogList=essayService.list(map);
		Long total=essayService.getTotal(map);
		JSONObject result=new JSONObject();
		JsonConfig jsonConfig=new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray=JSONArray.fromObject(blogList,jsonConfig);
		result.put("rows", jsonArray);
		result.put("total", total);
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 删除心情随笔记录
	 * @param ids
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public String delete(@RequestParam(value="ids")String ids,HttpServletResponse response)throws Exception{
		String []idsStr=ids.split(",");
		for(int i=0;i<idsStr.length;i++){
			essayService.delete(idsStr[i]);
		}
		JSONObject result=new JSONObject();
		result.put("success", true);
		ResponseUtil.write(response, result);
		return null;
	}
	
	/**
	 * 通过ID查找实体
	 * @param id
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findById")
	public String findById(@RequestParam(value="id")String id,HttpServletResponse response)throws Exception{
		Essay essay=essayService.findById(id);
		JSONObject jsonObject=JSONObject.fromObject(essay);
		ResponseUtil.write(response, jsonObject);
		return null;
	}
}
